package com.wondersgroup.fjzlpt.console.dao.impl.menu;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.console.dao.inter.menu.MenuDAO;

@Repository
public class MenuDAOImpl extends BaseJTDAOImpl implements MenuDAO {

	@Override
	@Autowired
	@Qualifier("TB_AUTH_MENU")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	@Autowired
	@Qualifier("TB_AUTH_MENU_ACTION")
	private TableBean tbMenuAction;

	@Override
	public JSONArray queryAll() {
		StringBuilder sbud = new StringBuilder("select a.*, b.menuname as pre_menuname from ").append(this.tb.getTableName()).append(" a \n")
				.append("left join ").append(this.tb.getTableName()).append(" b on a.pre_menuid=b.menuid \n")
				.append("order by a.MENU_ORDER, a.MENUID");//MENUID, MENU_LEVEL, MENU_ORDER
		SQLBean queryAllBean = new SQLBean(sbud.toString());
		return super.queryList(queryAllBean);
	}

	@Override
	public JSONArray queryMenuAndActions() {
		StringBuilder sbud = new StringBuilder("select a.*, b.menuname as pre_menuname, c.actions from ").append(this.tb.getTableName()).append(" a \n")
				.append("left join ").append(this.tb.getTableName()).append(" b on a.pre_menuid=b.menuid \n")
				.append("left join ").append(this.tbMenuAction.getTableName()).append(" c on a.menuid=c.menuid \n")
				.append("order by a.MENU_ORDER, a.MENUID");
		SQLBean queryAllBean = new SQLBean(sbud.toString());
		return super.queryList(queryAllBean);
	}
	
	/**
	 * 判断菜单ID是否存在
	 */
	public boolean isExistMenuId(String menuId)
	{
		StringBuilder sbud = new StringBuilder("select count(1) from ").append(this.tb.getTableName()).append(" where 1=1 and MENUID=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{menuId});
		return this.isExist(sb);
	}
}
